<?php
    //错误和异常处理
?>
<?php
    try {
        //抛出异常
        //throw是关键字
        throw new Exception("A terrible error has occured - 发生了一个可怕的错误", 42);
    }
    catch (Exception $e) {
        echo "Exception - 例外".$e->getCode().//返回错误代码
        "：".$e->getMessage().//返回错误信息
        "<br />"."in".$e->getFile().//返回异常文件代码的完整路径
        " on line ".$e->getLine()."<br />";//返回产生异常代码的行号
    };
    echo $e;
?>

<?php 
//user_defined_exception.php
//用户自定义Exception类的示例
    class myException extends Exception
    {
        function _toString()
        {
            return "<strong>Exception ".$this->getCode()
            ."</strong>:".$this->getMessage()."<br />"
            ."in ".$this->getFile()." on line ".$this->getLine()."<br />";
        }
    }

    try
    {
        throw new myException("A terrible error has occurred - 有一个致命错误哦", 42);
    }

    catch (myException $m)
    {
        echo $m;
    }
?>